﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dunnice.BlogIndex.Entity;
using Dunnice.Utility;
using Dunnice.BlogIndex.DLL;

namespace Dunnice.BlogIndex.BLL
{
    /// <summary>
    /// 博客作者信息管理.
    /// </summary>
    public class BlogBizControl
    {
        /// <summary>
        /// 获取所有的博客服务器信息.
        /// </summary>
        /// <returns></returns>
        public static List<BlogServerInfo> GetBlogServerInfos()
        {
            List<BlogServerInfo> lstInfo = new List<BlogServerInfo>();

            Dictionary<BlogServerEnum, BlogServerAttribute> dicServer =
                ReflectHelper.GetAttributeEnumInfo<BlogServerEnum, BlogServerAttribute>();

            foreach (KeyValuePair<BlogServerEnum, BlogServerAttribute> pair in dicServer)
            {
                lstInfo.Add(new BlogServerInfo()
                {
                    EnumValue = pair.Key.ToString(),
                    ServerDesc = pair.Value.BlogServerChsName,
                    ServerUrl = pair.Value.BlogDefaultUrl,
                    ServerType = pair.Key
                });
            }

            return lstInfo;
        }

        /// <summary>
        /// 格式化博客Url
        /// </summary>
        /// <param name="serverInfo"></param>
        /// <param name="author"></param>
        /// <returns></returns>
        public static string FormatBlogUrl(BlogServerInfo serverInfo, string author)
        {
            BlogServerEnum bs = (BlogServerEnum)Enum.Parse(typeof(BlogServerEnum), serverInfo.EnumValue, true);

            string res = string.Empty;

            switch (bs)
            {
                case BlogServerEnum.CnBlogs:
                    res = serverInfo.ServerUrl + "/" + author;
                    break;

                case BlogServerEnum.Csdn:
                    res = serverInfo.ServerUrl + "/" + author;
                    break;
            }

            return res;
        }

        /// <summary>
        /// 获取所有博客作者信息.
        /// </summary>
        /// <returns></returns>
        public static List<BlogAuthorInfo> GetBlogAuthorInfos()
        {
            return BlogDatabase.Instance.GetBlogAuthorInfos();
        }

        /// <summary>
        /// 存储博客作者信息.
        /// </summary>
        /// <param name="info"></param>
        public static void SaveBlogAuthorInfo(BlogAuthorInfo info)
        {
            BlogDatabase.Instance.SaveBlogAuthorInfo(info);
        }

        /// <summary>
        /// 保存博客文章信息.
        /// </summary>
        /// <param name="info"></param>
        public static void SaveArticleInfo(BlogArticleInfo info)
        {
            BlogDatabase.Instance.SaveArticleInfo(info);
        }

        /// <summary>
        ///  获取指定博主的博客文章列表.
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        public static List<BlogArticleInfo> GetArticleInfos(BlogAuthorInfo author)
        {
            return BlogDatabase.Instance.GetArticleInfos(author.ServerType, author.AuthorName);
        }

        /// <summary>
        /// 获取指定博主的所有博客文章列表.
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        public static List<BlogArticleInfo> GetArticleContents(BlogAuthorInfo author)
        {
            return BlogDatabase.Instance.GetArticleInfos(author.ServerType, author.AuthorName, true);
        }
    }
}
